package api

import (
	"context"

	controller "gitee.com/klein-x/biz/controller/system"
	"gitee.com/klein-x/biz/xutils"
	"github.com/cloudwego/hertz/pkg/app"
)

type SystemInfoRequest struct {
	Version Version `path:"version" vd:"in($, 1, 2); msg:'未定义的请求'"`
}
type SystemInfoResponse struct {
	Info []controller.SystemInfo `json:"system"`
}

func SystemInfo(ctx context.Context, c *app.RequestContext) {
	var req SystemInfoRequest
	if err := c.BindAndValidate(&req); err != nil {
		ReturnError(c, xutils.Error(API_PARAM_ERROR, err))
		return
	}

	if info, err := controller.GetAllSystem(ctx); err != nil {
		ReturnError(c, err)
		return
	} else {
		var resp SystemInfoResponse
		resp.Info = info
		ReturnSuccess(c, resp)
	}
}

type SystemBannerRequest struct {
	Version Version `path:"version" vd:"in($, 1, 2); msg:'未定义的请求'"`
	System  string  `form:"system" json:"system"`
}
type SystemBannerResponse struct {
	Banner []string `json:"Banner"`
}

func SystemBannerInfo(ctx context.Context, c *app.RequestContext) {
	var req SystemBannerRequest
	if err := c.BindAndValidate(&req); err != nil {
		ReturnError(c, xutils.Error(API_PARAM_ERROR, err))
		return
	}

	if bannerInfo, err := controller.GetSystemBanners(ctx, req.System); err != nil {
		ReturnError(c, err)
		return
	} else {
		var resp SystemBannerResponse
		resp.Banner = bannerInfo
		ReturnSuccess(c, resp)
	}
}

type SystemGoodsRequest struct {
	Version Version `path:"version" vd:"in($, 1, 2); msg:'未定义的请求'"`
	System  string  `form:"system" json:"system"`
}
type SystemGoodsResponse struct {
	GoodsSummary []controller.GoodsSummary `json:"goods"`
}

func SystemGoodsSummaryList(ctx context.Context, c *app.RequestContext) {
	var req SystemGoodsRequest
	if err := c.BindAndValidate(&req); err != nil {
		ReturnError(c, xutils.Error(API_PARAM_ERROR, err))
		return
	}

	if goodsInfo, err := controller.GetSystemGoodsSummarys(ctx, req.System); err != nil {
		ReturnError(c, err)
		return
	} else {
		var resp SystemGoodsResponse
		resp.GoodsSummary = goodsInfo
		ReturnSuccess(c, resp)
	}
}

type SystemGoodsDetailRequest struct {
	Version Version `path:"version" vd:"in($, 1, 2); msg:'未定义的请求'"`
	GoodsId string  `form:"goodsId" json:"goodsId"`
}

type SystemGoodsDetailResponse struct {
	GoodsDeatil *controller.GoodsDetail `json:"goods_detail"`
}

func SystemGoodsDetail(ctx context.Context, c *app.RequestContext) {
	var req SystemGoodsDetailRequest
	if err := c.BindAndValidate(&req); err != nil {
		ReturnError(c, xutils.Error(API_PARAM_ERROR, err))
		return
	}

	if goodsDetailInfo, err := controller.GetGoodsDetail(ctx, req.GoodsId); err != nil {
		ReturnError(c, err)
		return
	} else {
		var resp SystemGoodsDetailResponse
		resp.GoodsDeatil = goodsDetailInfo
		ReturnSuccess(c, resp)
	}
}
